﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Transactions;
using biquge5200.Common;
using DbHelper;
using HtmlHelper;
using Models.SqlitModel;

namespace biquge5200
{
    internal class Program
    {
        private static SqliteEFHelper Sqlite;
        public Program()
        {
            Sqlite = new SqliteEFHelper();
        }

        private static void Main(string[] args)
        {
            GetXiaoShuo();
            //GetZhangJieMenu();
            Console.ReadLine();
        }
        /// <summary>
        /// 获取所有小说
        /// </summary>
        public static void GetXiaoShuo()
        {
            var html = ParserHtml.LoadHtml("http://www.biquge5200.com/xiaoshuodaquan/");
            var list = GetList.GetAllFiction(html);
            foreach (var iteMenuse in list)
            {
                if (!Sqlite.FindAll<Books>(o => o.Name == iteMenuse.Text && o.Href == iteMenuse.Href).Any())
                {
                    var xs = new Books()
                    {
                        Id = Guid.NewGuid().ToString(),
                        Name = iteMenuse.Text,
                        Href = iteMenuse.Href,
                        State = "未下载",
                        AddTime = DateTime.Now
                    };
                    xs = Sqlite.Insert(xs);
                    Console.WriteLine(xs.Name + " 写入成功 " + xs.Id);
                }
                else
                {
                    Console.WriteLine(iteMenuse.Text + " 已存在 ");
                }
            }
        }

        private static int retrytimes = 0;

        /// <summary>
        /// 获取小说章节目录
        /// </summary>
        public static void GetZhangJieMenu(Books xiaoShuo = null)
        {
            Books xs = new Books();
            try
            {
                if (xiaoShuo == null)
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        xs = Sqlite.FindAll<Books>(o => o.State == "未下载").FirstOrDefault();
                        xs.State = "下载中";
                        Sqlite.Update(xs);
                        ts.Complete();
                    }
                }
                Console.WriteLine("开始下载》》》" + xs.Name);
                var html = ParserHtml.LoadHtml(xs.Href);
                Console.WriteLine("下载成功，开始解析。");
                var list = GetList.GetMenu(html) ?? new List<Menus>(); ;
                Console.WriteLine("解析成功，共" + list.Count + "个章节。");
                foreach (var item in list)
                {
                    BooksChapter zj = new BooksChapter()
                    {
                        Id = Guid.NewGuid().ToString(),
                        Name = item.Text,
                        Href = item.Href,
                        State = "未下载",
                        AddTime = DateTime.Now,
                        Code = item.Id,
                        XsId = xs.Id
                    };
                    zj = Sqlite.Insert(zj);
                    Console.WriteLine("章节 " + zj.Name + " 写入数据库成功");
                }
                xs.State = "下载完成";
                Sqlite.Update(xs);
            }
            catch (Exception e)
            {
                Console.WriteLine("程序出错》》休息一下");
                Thread.Sleep(30000);
                using (TransactionScope ts = new TransactionScope())
                {
                    xs.State = "下载失败";
                    Sqlite.Update(xs);
                    ts.Complete();
                }
                retrytimes++;
                if (retrytimes <= 5)
                    GetZhangJieMenu(xs);
                else
                    retrytimes = 0;
            }
            GetZhangJieMenu();
        }

        public void GetZhangJie()
        {

        }
    }
}
